<?php

namespace app\tours\controller;

use think\Controller;

class Common extends Controller {

        public function _initialize() {
                //$_REQUEST = array_merge($_GET, $_POST);
                //清除cookie
                if (session('uid') != cookie('uid')) {
                        cookie('uid', null);
                }
                //自动登录
                if (cookie('auth_key')) {
                        $auth_key = cookie('auth_key');
                        if (pwSha($auth_key['uid']) == $auth_key['key']) {
                                session("uid", $auth_key['uid']);
                                $user = D("User")->relation(true)->where(array('id' => $auth_key['uid']))->find();
                                if (!empty($user['realname'])) {
                                        $userName = $user['realname'];
                                } else {
                                        $userName = $user['username'];
                                }
                                session('username', $userName);
                                cookie('uid', $auth_key['uid']);
                                cookie('username', $userName);
                        } else {
                                cookie('auth_key', null);
                        }
                }

                //页脚导航
                $aboutUs = M("Aboutus");
                $aboutNav = $aboutUs->cache()->field(true)->order("id asc")->select();
                $this->assign('about', $aboutNav);

                //主导航
                $navigation = explode("\n", C('site.navigation'));
                foreach ($navigation as $key => $value) {
                        $value = explode("|", $value);
                        $value[1] = str_replace(array("\r\n", "\r", "\n"), "", $value[1]);
                        $navigation[$key] = array('name' => $value[0], 'url' => $value[1]);
                }
                $this->assign("navigation", $navigation);

                //友情链接
                $linklist = M("Link")->cache()->field('name,address')->select();
                $this->assign('linklist', $linklist);

                //统计代码 QQ客服
                $travelv = M("Travelv")->cache()->field('name,value')->select();
                $tra = array();
                foreach ($travelv as $key => $val) {
                        $tra[$val['name']] = $val;
                }

                if (!empty($tra['logo']['value'])) {
                        $logo = $tra['logo']['value'];
                        $this->assign('logo', $logo);
                }
                if (!empty($tra['erweima']['value'])) {
                        $erweima = $tra['erweima']['value'];
                        $this->assign('erweima', $erweima);
                }
                if (!empty($tra['statis']['value'])) {
                        $statis = $tra['statis']['value'];
                        $this->assign('statis', $statis);
                }
                if (!empty($tra['qq']['value'])) {
                        $tenx = unserialize($tra['qq']['value']);
                        $this->assign('tenx', $tenx);
                }
                if (!empty($tra['tall']['value'])) {
                        $call = unserialize($tra['tall']['value']);
                        $this->assign('call', $call);
                }
        }

        /**
         * 空方法
         */
        public function _empty() {
                send_http_status(404);
                return $this->fetch("public:_404");
        }

        /**
         * Ajax方式返回数据到客户端
         * @access protected
         * @param mixed $data 要返回的数据
         * @param String $type AJAX返回数据格式
         * @return void
         */
        protected function ajaxReturn($data, $type = '') {
                if (func_num_args() > 2) {// 兼容3.0之前用法
                        $args = func_get_args();
                        array_shift($args);
                        $info = array();
                        $info['data'] = $data;
                        $info['info'] = array_shift($args);
                        $info['status'] = array_shift($args);
                        $data = $info;
                        $type = $args ? array_shift($args) : '';
                }
                if (empty($type))
                        $type = C('DEFAULT_AJAX_RETURN');
                switch (strtoupper($type)) {
                        case 'JSON' :
                                // 返回JSON数据格式到客户端 包含状态信息
                                header('Content-Type:application/json; charset=utf-8');
                                exit(json_encode($data));
                        case 'XML' :
                                // 返回xml格式数据
                                header('Content-Type:text/xml; charset=utf-8');
                                exit(xml_encode($data));
                        case 'JSONP':
                                // 返回JSON数据格式到客户端 包含状态信息
                                header('Content-Type:application/json; charset=utf-8');
                                $handler = isset($_GET[C('VAR_JSONP_HANDLER')]) ? $_GET[C('VAR_JSONP_HANDLER')] : C('DEFAULT_JSONP_HANDLER');
                                exit($handler . '(' . json_encode($data) . ');');
                        case 'EVAL' :
                                // 返回可执行的js脚本
                                header('Content-Type:text/html; charset=utf-8');
                                exit($data);
                        default :
                                // 用于扩展其他返回格式数据
                                tag('ajax_return', $data);
                }
        }

        /**
         * 检查是否登陆
         */
        protected function checkUser() {
                $uid = session('uid');
                if (empty($uid)) {
                        if (!IS_AJAX) {
                                session('last_url', get_url());
                        }
                        $this->redirect("/user/login");
                        return;
                }
        }

        /**
         * last_url获取父页面链接
         */
        protected function checkFatherUrl() {
                $fatherUrl = $_SERVER['HTTP_REFERER'];
                session('last_url', $fatherUrl);
        }

}

/**
 * 用正则表达式验证手机号码(中国大陆区)
 * @param integer $num    所要验证的手机号
 * @return boolean
 */
function isMobile($num) {
        if (!$num) {
                return false;
        }
        return preg_match('#^13[\d]{9}$|14^[0-9]\d{8}|^15[0-9]\d{8}$|^18[0-9]\d{8}|^17[0-9]\d{8}$#', $num) ? true : false;
}

/**
 * 正则表达式验证邮箱
 */
function isEmail($email) {
        if (!$email) {
                return false;
        }
        return preg_match("/^[0-9a-zA-Z]+@(([0-9a-zA-Z]+)[.])+[a-z]{2,4}$/i", $email) ? true : false;
}
